package com.app.shop.activitys;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.app.library.base.BaseActivity;
import com.app.library.beans.EventMessage;
import com.app.library.utils.DialogUtil;
import com.app.library.utils.GlideUtils;
import com.app.library.utils.StringUtil;
import com.app.library.utils.TimeUtil;
import com.app.library.utils.ToastUtil;
import com.app.library.utils.UserManager;
import com.app.library.widgts.CustomDialog;
import com.app.shop.R;
import com.app.shop.activitys.DTEditActivity;
import com.app.shop.adapters.TalkAdapter;
import com.app.shop.beans.DTBean;
import com.app.shop.beans.TalkBean;


import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class DTDetailActivity extends BaseActivity implements View.OnClickListener{


    private    TextView tvTitle;

    private  ImageView imgvPic;

    private  TextView tvName;

    private TextView tvUser;

    private    TextView tvTime;

    private   TextView tvDescribe;
    private  EditText edt;
    DTBean bean;
    Bundle bundle;
    TextView tvRight;
    RecyclerView recy;
    private String dialog_title = "删除商品";
    private String dialog_content = "是否确定删除？";
    private CustomDialog customDialog;
    private  ImageView   imgvreturn;
    private  TextView   tvbt;
    TalkAdapter adapter;
    List<TalkBean> list = new ArrayList<>();
    List<TalkBean> list_all = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dt_detail);
        tvRight= (TextView) findViewById(R.id.tv_add);
        recy= (RecyclerView) findViewById(R.id.recy);
        edt= (EditText) findViewById(R.id.edt);
        tvDescribe= (TextView) findViewById(R.id.tv_describe);
        tvTime= (TextView) findViewById(R.id.tv_time);
        tvUser= (TextView) findViewById(R.id.tv_user);
        tvName= (TextView) findViewById(R.id.tv_dt_title);
        imgvPic= (ImageView) findViewById(R.id.imgv_pic);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        tvbt= (TextView) findViewById(R.id.tv_bt);
        tvRight.setOnClickListener(this::onClick);
        imgvreturn.setOnClickListener(this::onClick);
        tvbt.setOnClickListener(this::onClick);
        tvTitle.setText("动态详情");
        initData();
        initTalk();
    }

    private void initTalk() {
        list_all = DataSupport.findAll(TalkBean.class);
        if (list_all.size() > 0) {
            for (int i = 0; i < list_all.size(); i++) {
                if (list_all.get(i).getQues_id().equals(bean.d_id)) {
                    list.add(list_all.get(i));
                }
            }
        }

        Collections.reverse(list); // 倒序排列

        adapter = new TalkAdapter(R.layout.item_list_talk, list);
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);

    }

    private void initData() {
        bundle = getIntent().getExtras();
        bean = (DTBean) bundle.getSerializable("bean");
        setView();
    }

    private void setView() {
        if (bean.user_id.equals(UserManager.getUserId(this))) {
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText("编辑");
        } else {
            tvRight.setVisibility(View.GONE);
        }
        GlideUtils.getInstance().loadImage(this, bean.pic, imgvPic);
        tvName.setText(bean.title);
        tvUser.setText("用户：" + bean.user_name);
        tvTime.setText("发布时间：" + bean.create_time);
        tvDescribe.setText(bean.content);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.tv_add:

                if (!UserManager.isLogin(this)) {
                    ToastUtil.showToast(this, "请登陆");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", bean);
                showActivity(this, DTEditActivity.class, bundle);
                break;

            case R.id.tv_bt:

                if (StringUtil.isEmpty(edt.getText().toString())) {
                    ToastUtil.showToast(this, "请输入内容");
                    return;
                }

                TalkBean talkBean = new TalkBean();
                talkBean.setCreate_time(TimeUtil.getTodayData("yyyy-MM-dd HH:mm:ss"));
                talkBean.setTalk_id(System.currentTimeMillis() + "");
                talkBean.setUser(UserManager.getUserName(this));
                talkBean.setUser_id(UserManager.getUserId(this));
                talkBean.setContent(edt.getText().toString());
                talkBean.setQues_id(bean.d_id);
                talkBean.save();

                list.add(0, talkBean);
                adapter.notifyDataSetChanged();


                edt.setText("");

                break;
        }
    }


    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            if (msg.getmObject() != null) {
                bean = (DTBean) msg.getmObject();
                setView();
            }
        }
    }

    private void del() {

        customDialog = DialogUtil.showDialog(this, customDialog, 2, dialog_title, dialog_content, "取消", "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

                DataSupport.deleteAll(DTBean.class, "d_id=?", bean.d_id);
                EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
                ToastUtil.showToast(DTDetailActivity.this, "删除成功");
                finish();
            }
        });

        if (customDialog != null && !customDialog.isShowing()) {
            customDialog.show();
        }


        customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                customDialog = null;
            }
        });

    }


}
